package com.imax.emap.mapProvider
{
	import com.imax.emap.projection.IProjection;
	import com.imax.emap.projection.LingtuProjection;

	public class LingtuProvider implements IMapProvider
	{
		private var imgSize:int = 200;
		private var imgNtuSize:int = 256;
		private var zoom:int;
		private var projection:IProjection;
		/**
		 * 灵图官网太tmd慢了 "http://dimg.51ditu.com/"
		 */		
		private var imgURL:Array=["http://mapimage.koubei.com/map/"];
		
		public function LingtuProvider()
		{
			this.projection = new LingtuProjection();
		}

		public function getTileSize():Number
		{
			//灵图的图片大小是200×200
			return imgSize;
		}
		
		public function getProjection():IProjection
		{
			return projection;
		}
		
		public function getTileURL(x:int, y:int, zoom:Number):String
		{
			var z:Number=this.get51dituZoom(zoom);
			var nGrade:Number=Math.floor(Math.ceil( (12-z)/4 ));
			var nPreRow:Number=0,nPreCol:Number=0,nPreSize:Number=0;  
			var path:String="";
			for(var i:Number=0;i<nGrade;i++)
			{
				var nSize:Number=1<<(4*(nGrade-i));
				var nRow:Number =Math.floor((x-nPreRow*nPreSize)/nSize);
				var nCol:Number =Math.floor((-y-1-nPreCol*nPreSize)/nSize);
				path+=((nRow>9)?nRow:"0"+nRow)+""+((nCol>9)?nCol:"0"+nCol)+"/";
				nPreRow = nRow;
				nPreCol = nCol;
				nPreSize = nSize;
			}
			var id:Number=(((x)&((1<<20)-1))+(((-y-1)&((1<<20)-1))*Math.pow(2,20))+(((z)&((1<<8)-1))*Math.pow(2,40)));
			
			trace("tilePos:("+x+","+y+") tileUrl:"+this.imgURL[(x+y)%this.imgURL.length]+z+"/"+path+id+".png");
			return this.imgURL[(x+y)%this.imgURL.length]+z+"/"+path+id+".png";
		}
		
		public function toString():String
		{
			return "51ditu";
		}
		
		public function getMaxZoom():int
		{
			return 17;
		}
		
		public function getMinZoom():int
		{
			return 1;
		}
		
		private function get51dituZoom(_zoom:Number):int{
			return 17-_zoom;
		}
		
	}
}